//@filename:RCS_usart.h
//@author:  Morris Zhang
//@date:    18-August-2012
//@brief:   USART涓插彛閫氫俊  @brief鍑芥暟灏佽�呯被搴�

#ifndef _RCS_USART_H_
#define _RCS_USART_H_

// C语言公共库
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>

// BSP与中间件
#include "stm32f4xx.h"      //STM32寄存器定义
#include "stm32f4xx_conf.h" //STM32标准库头文件
#include "bsp.h"            //uCOS底层组件
#include "includes.h"       //uCOS必备组件
#include "delay.h"          //延时函数

// RCSLIB硬件抽象层
#include "RCS_Types.h"       //获取MCU的外设-时钟连接关系
#include "RCS_HAL.h"         //获取MCU的寄存器定义
#include "RCS_Pin_Mapping.h" //主控板的引脚映射和外设映射

//@name: USART_Config
//@brief: 閰嶇疆USART鐨勪覆鍙ｉ€氫俊@brief
//@param: _USARTx          闇€瑕佺粦瀹氱殑USART涓插彛
//@param: _GPIOx           闇€瑕佺粦瀹氱殑GPIO缁?
//@param: _GPIO_PinX_T     闇€瑕佺粦瀹氱殑涓插彛鍙戦€佺�¤�?
//@param:_GPIO_PinX_R      闇€瑕佺粦瀹氱殑涓插彛鎺ユ敹绠¤剼
//@param: _intFuc          鎺ユ敹涓�鏂�鍑芥暟鐨勬寚閽?
//@param:_baudRate         浼犻€佺殑娉㈢壒鐜?
//@param: _pri             浼樺厛绾?
//@note:娓呴櫎涓�鏂�鏍囧織浣?
// USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
void RCS_USART_Config(USART_TypeDef *_USARTx, GPIO_TypeDef *_GPIOx, uint32_t _GPIO_PinX_T,
                      uint32_t _GPIO_PinX_R, FNCT_VOID _intFuc, uint32_t _baudRate, uint8_t _pri);
void RCS_USART_With_NoIsr_Config(USART_TypeDef *_USARTx, GPIO_TypeDef *_GPIOx, uint32_t _GPIO_PinX_T,
                                 uint32_t _GPIO_PinX_R, uint32_t _baudRate);

//@name: RCS_USART_Send_Char
//@brief: 閫氳繃宸插畾涔夌殑涓插彛鍙戦€佷竴涓�瀛楃�?
//@param:_USARTx     宸茬粡缁戝畾鐨刄SART涓插彛
//@param:_character  瑕佸彂閫佺殑瀛楃��

__inline void RCS_USART_Send_Char(USART_TypeDef *_USARTx, uint8_t _character)
{
    USART_SendData(_USARTx, (uint16_t)_character & 0x00ff);
    while (USART_GetFlagStatus(_USARTx, USART_FLAG_TC) == RESET);
}

//@name: RCS_USART_Send_Str
//@brief: 閫氳繃宸插畾涔夌殑涓插彛鍙戦€佷竴涓�瀛楃�︿覆
//@param:_USARTx   宸茬粡缁戝畾鐨刄SART涓插彛
//@param:_TxBuffer 瑕佸彂閫佺殑瀛楃�︿覆鏁扮�?
__inline void RCS_USART_Send_Str(USART_TypeDef *_USARTx, uint8_t _TxBuffer[])
{
    uint8_t i = 0;
    while (USART_GetFlagStatus(_USARTx, USART_FLAG_TC) == RESET);
    for (; _TxBuffer[i] != '\0'; i++) {
        USART_SendData(_USARTx, (uint16_t)_TxBuffer[i]);
        while (USART_GetFlagStatus(_USARTx, USART_FLAG_TC) == RESET);
    }
}

//@name: RCS_USART_Send_Data
//@brief: 閫氳繃宸插畾涔夌殑涓插彛鍙戦€佷竴涓叉暟鎹?
//@param:_USARTx 宸茬粡缁戝畾鐨刄SART涓插彛
//@param:address 鏁版嵁璧峰�嬪湴鍧€
//@param:length  鏁版嵁闀垮害
__INLINE void RCS_USART_Send_Data(USART_TypeDef *_USARTx, uint8_t *address, uint8_t length)
{
    uint8_t i = 0;
    while (USART_GetFlagStatus(_USARTx, USART_FLAG_TC) == RESET);
    for (; i < length; i++) {
        USART_SendData(_USARTx, (uint16_t)*(address + i));
        while (USART_GetFlagStatus(_USARTx, USART_FLAG_TC) == RESET);
    }
}
//@name: RCS_USART_Accept_Char
//@brief: 閫氳繃宸插畾涔夌殑涓插彛鎺ユ敹涓€涓�瀛楃�?
//@param:_USARTx 宸茬粡缁戝畾鐨刄SART涓插彛
//@retval: uint16浣嶇殑瀛楃��
__INLINE uint16_t RCS_USART_Accept_Char(USART_TypeDef *_USARTx)
{
    return USART_ReceiveData(_USARTx);
}

__INLINE void RCS_USART_Clear_RcvCplt_ITPendingBit(USART_TypeDef *_USARTx)
{
    USART_ClearITPendingBit(_USARTx, USART_IT_RXNE);
}

__INLINE int RCS_USART_Judge_RcvCplt_ITPendingBit(USART_TypeDef *_USARTx)
{
    if (USART_GetITStatus(_USARTx, USART_IT_RXNE) == SET)
        return 1;
    else
        return 0;
}

#endif //_RCS_USART_H_
